package com.wjqy.controller;

import com.alibaba.fastjson.JSONObject;
import com.wjqy.entity.CommonReturnEntity;
import com.wjqy.entity.Scenic;
import com.wjqy.entity.ScenicReturnEntity;
import com.wjqy.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("admin")
public class templateController {

    @Autowired
    private AdminService adminService;

    @ResponseBody
    @RequestMapping("findallscenic")
    public ScenicReturnEntity findAllScenic() {
        List<Scenic> allScenic = adminService.findAllScenic();
        ScenicReturnEntity rs = new ScenicReturnEntity();
        rs.setData(allScenic);
        rs.setCode(0);
        rs.setCount(adminService.countScenic());
        return rs;
    }

    @ResponseBody
    @RequestMapping("sceniclike")
    public ScenicReturnEntity findScenicLike(@RequestParam("searchParams") String s) {
        JSONObject jsonObject = JSONObject.parseObject(s);
        String name = jsonObject.getString("name");
        String localtion = jsonObject.getString("localtion");
        Integer id = jsonObject.getInteger("id");
        Scenic scenic = new Scenic();
        scenic.setId(id);
        scenic.setName(name);
        scenic.setLocaltion(localtion);
        System.out.println(scenic);
        List<Scenic> scenics = adminService.findScenicLike(scenic);
        System.out.println(scenics);
        if (scenics!=null){
            return new ScenicReturnEntity(0,scenics,0);
        }
       return null;
    }

    @ResponseBody
    @RequestMapping(value = "scenic",method = RequestMethod.POST)
    public CommonReturnEntity insertScenic(Scenic scenic){
        int i = adminService.insertScenic(scenic);
        if (i>0){
            return CommonReturnEntity.success("添加数据成功");
        }
        return CommonReturnEntity.fail("添加数据失败");
    }
    @ResponseBody
    @RequestMapping(value = "scenic",method = RequestMethod.PUT)
    public CommonReturnEntity updateScenic(Scenic scenic){
        System.out.println(scenic);
        int i = adminService.updateScenic(scenic);
        if (i>0){
            return CommonReturnEntity.success("更新成功");
        }
        return CommonReturnEntity.fail("更新失败");
    }


    @ResponseBody
    @RequestMapping(value = "scenic",method = RequestMethod.DELETE)
    public CommonReturnEntity deleteScenic(Integer id){
        int i = adminService.deleteScenic(id);
        if (i>0){
            return CommonReturnEntity.success("删除成功");
        }
        return CommonReturnEntity.fail("删除失败");
    }

    @RequestMapping("homepage")
    public String homePage() {
        return "admin/index";
    }

    @RequestMapping("welcome")
    public String welcom() {
        return "admin/welcome";
    }

    @RequestMapping("scenic")
    public String scenic() {
        return "admin/scenic";
    }

    @RequestMapping("block")
    public String ceshi() {
        return "admin/block";
    }

    @RequestMapping("scenicadd")
    public String add() {
        return "admin/table/scenicadd";
    }

    @RequestMapping("scenicedit")
    public String edit() {
        return "admin/table/scenicedit";
    }

    @RequestMapping("hello")
    @ResponseBody
    public String hello(String name) {
        return name;
    }



}
